package cn.cyl.org.service.impl;

import cn.cyl.basic.exception.BusinessException;
import cn.cyl.basic.service.impl.BaseServiceImpl;
import cn.cyl.basic.util.BaiduAi;
import cn.cyl.org.domain.Employee;
import cn.cyl.org.domain.Shop;
import cn.cyl.org.domain.ShopAuditLog;
import cn.cyl.org.dto.ShopDto;
import cn.cyl.org.mapper.EmployeeMapper;
import cn.cyl.org.mapper.ShopAuditLogMapper;
import cn.cyl.org.mapper.ShopMapper;
import cn.cyl.org.service.ShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Transactional
    @Override
    public void settlement(Shop shop) {

        //一.校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("信息不能为空");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码不相同");
        }

        //3.当前店铺是否入驻过

        Shop dbshop = shopMapper.findByNameAndAddress(shop);
        if(dbshop!= null){
            throw new BusinessException("店铺已经入驻过了");
        }

        //二.百度AI智能审核[店铺名，Logo是否合法]
        Boolean textCensor = BaiduAi.TextCensor(shop.getName());
        if(!textCensor){
            throw new BusinessException("店铺名不合规");
        }

        Boolean imgCensor = BaiduAi.ImgCensor("http://123.207.27.208"+shop.getLogo());
        if(!imgCensor){
            throw new BusinessException("店铺Logo不合规");
        }

        //三: 添加到数据库

        // 1.先添加员工信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);
        // 2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        // 3.将店铺d更新到员工表中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);

        //四: 人工审核之后发送激活邮件


    }
    //审核成功
    @Transactional
    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核通过待激活【1.先查再改 2.直接修改】
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);//审核通过
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他去激活
        //创建复杂邮件对象
        Shop shop = shopMapper.loadOne(log.getShopId());
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1175864373@qq.com");
        helper.setSubject("店铺激活邮件");
            helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);

        //收件人
        Employee employee = employeeMapper.loadOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    //审核失败
    @Transactional
    @Override
    public void auditReject(ShopAuditLog log) {
        //1.改状态：1待审核 - 4审核失败
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);//审核失败
        log.setAuditId(350L);//这里写死，因为还没有做登录
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他重新修改信息入驻
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1175864373@qq.com");
        message.setTo("1215818067@qq.com");
        message.setSubject("店铺入驻失败邮件");
        message.setText("入驻失败，请重新确认信息后再试");
        javaMailSender.send(message);

    }
    @Transactional
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.loadOne(id);
        shop.setState(3);
        shopMapper.update(shop);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
